<template>
  <div id="app" class="unusualJob">
    <div class="main">
      <div class="nav">
      </div>
      <div class="cont" v-loading="loading" element-loading-text="拼命加载中">
        <ul>
          <li class="firstLi">
            <div>事件ID</div>
            <div>服务名</div>
            <div>任务名</div>
            <div>开始时间</div>
            <div>结束时间</div>
            <div>失败重试次数</div>
            <div>任务状态</div>
            <div>操作</div>
          </li>
          <li class="dataLi" v-for="(item,index) in items" v-show="isDatas">
            <div>{{item.eventId}}</div>
            <div>{{item.appName}}</div>
            <div>{{item.jobName}}</div>
            <div>{{item.startTime | ctimeFun}}</div>
            <div>{{item.endTime | ctimeFun}}</div>
            <div>{{item.repeatCount}}</div>
            <div>{{item.state | stateFun}}</div>
            <div>
              <small @click="deleteClick(index)">
                     <span class="glyphicon glyphicon-trash"></span>删除
              </small>
            </div>
          </li>
          <li class="nullLi" v-show="!isDatas">
            <div>暂时没有数据</div>
          </li>
          <li class="lastLi">
            <div class="lastSel">
              <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 30, 40]" :page-size="pageSizeNum"
                :current-page="currentPage" layout="total, sizes, prev, pager, next" :total="totalNum">
                </el-pagination>
            </div>
            <div class="lastPag">
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
  import { mapActions } from 'vuex'
  export default {
    name: 'unusualJob',
    data() {
      return {
        loading: false,
        currentPage: 1,
        pageSizeNum: 10,
        totalNum: 0,
        isSeal: 0,
        items: [],
        isDatas: true
      }
    },
    sockets: {
      // 取消事件
      s_t_ts_c_w_job_cancel: function (data) {
        console.log('任务列表监听取消.', data)
        const h = this.$createElement;
        var dataObj = JSON.parse(data)
        this.$notify({
          title: dataObj.appName + ' 的任务已经取消',
          message: h('p', { style: 'color: #333' }, '事件ID：' + dataObj.eventId + '。' + '任务名称：' + dataObj.jobName)
        })
        var self = this
        // 获取完成或删除任务
        self.loading = true
        self.getAllList({
          pageNum: self.currentPage,
          pageSize: self.pageSizeNum,
        }).then((data) => {
          console.log(JSON.parse(JSON.stringify(data)))
          if (data.code === 200) {
            if (data.data.list.length > 0) {
              self.isDatas = true
              self.items = data.data.list
              self.totalNum = data.data.total
              self.loading = false
            } else {
              self.isDatas = false
              self.loading = false
            }
          }
        }).catch(msg => {
          console.log(msg)
          switch (msg.status) {
            case 500:
              break;
            case 504:
              break;
            default:
              break;
          }
        })
      },
      // 完成事件
      s_t_ts_c_w_job_complete: function (data) {
        console.log('任务列表监听完成.', data)
        const h = this.$createElement;
        var dataObj = JSON.parse(data)
        this.$notify({
          title: dataObj.appName + ' 的任务已经完成',
          message: h('p', { style: 'color: #333' }, '事件ID：' + dataObj.eventId + '。' + '任务名称：' + dataObj.jobName)
        })
        var self = this
        // 获取完成或删除任务
        self.loading = true
        self.getAllList({
          pageNum: self.currentPage,
          pageSize: self.pageSizeNum,
        }).then((data) => {
          console.log(JSON.parse(JSON.stringify(data)))
          if (data.code === 200) {
            if (data.data.list.length > 0) {
              self.isDatas = true
              self.items = data.data.list
              self.totalNum = data.data.total
              self.loading = false
            } else {
              self.isDatas = false
              self.loading = false
            }
          }
        }).catch(msg => {
          console.log(msg)
          switch (msg.status) {
            case 500:
              break;
            case 504:
              break;
            default:
              break;
          }
        })
      }
    },
    created: function () {
      var self = this
      // 获取完成或删除任务
      self.loading = true
      self.getAllList({
        pageNum: self.currentPage,
        pageSize: self.pageSizeNum,
      }).then((data) => {
        console.log(JSON.parse(JSON.stringify(data)))
        if (data.code === 200) {
          if (data.data.list.length > 0) {
            self.isDatas = true
            self.items = data.data.list
            self.totalNum = data.data.total
            self.loading = false
          } else {
            self.isDatas = false
            self.loading = false
          }
        }
      }).catch(msg => {
        console.log(msg)
        switch (msg.status) {
          case 500:
            break;
          case 504:
            break;
          default:
            break;
        }
      })
    },
    filters: {
      ctimeFun: function (val) {
        if (val) {
          var now = new Date(val)
          var year = now.getYear() - 100;
          var month = now.getMonth() + 1;
          month = month > 9 ? month : "0" + month
          var date = now.getDate();
          date = date > 9 ? date : "0" + date
          var hour = now.getHours();
          hour = hour > 9 ? hour : "0" + hour
          var minute = now.getMinutes();
          minute = minute > 9 ? minute : "0" + minute
          return "20" + year + "-" + month + "-" + date + "   " + hour + ":" + minute;
        } else {
          return '无'
        }
      },
      stateFun: function (val) {
        if (val == 0) {
          return '已完成'
        } else {
          return '已终止'
        }
      }
    },
    methods: {
      // 设置每页个数
      handleSizeChange(val) {
        var self = this
        self.loading = true
        self.getAllList({
          pageNum: self.pageSizeNum,
          pageSize: self.currentPage
        }).then((data) => {
          console.log(data)
          if (data.code === 200) {
            if (data.data.list.length > 0) {
              self.isDatas = true
              self.items = data.data.list
              self.totalNum = data.data.total
              self.loading = false
            } else {
              self.isDatas = false
              self.loading = false
            }

          }
        }).catch(msg => {
          console.log(msg)
          switch (msg.status) {
            case 500:
              break;
            case 504:
              break;
            default:
              break;
          }
        })
      },
      // 点击分页
      handleCurrentChange(val) {
        var self = this
        self.loading = true
        self.currentPage = val
        self.getAllList({
          pageNum: val,
          pageSize: self.pageSizeNum
        }).then((data) => {
          console.log(data)
          if (data.code === 200) {
            if (data.data.list.length > 0) {
              self.isDatas = true
              self.items = data.data.list
              self.totalNum = data.data.total
              self.loading = false
            } else {
              self.isDatas = false
              self.loading = false
            }
          }
        }).catch(msg => {
          console.log(msg)
          switch (msg.status) {
            case 500:
              break;
            case 504:
              break;
            default:
              break;
          }
        })
      },
      // 删除任务
      deleteClick(index) {
        var self = this
        var str = self.items[index].jobName ? self.items[index].jobName : ''
        var nameStr = "此操作将删除 " + str + " 该任务,是否继续 ?"
        this.$confirm(nameStr, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          self.loading = true
          self.getDelJob({
            jobId: self.items[index].id
          }).then((data) => {
            console.log(data)
            if (data.code === 200) {
              self.loading = false
            } else if (data.code == 3009) {
              self.loading = false
              self.$message('任务正在进行中。。。不能删除')
            }
          }).catch(msg => {
            console.log(msg)
            switch (msg.status) {
              case 500:
                break;
              case 504:
                break;
              default:
                break;
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      ...mapActions(['getAllList','getDelJob'])
    },

  }

</script>

<style scoped>
  .unusualJob {
    width: 100%;
    display: flex;
    justify-content: center;
    min-height: 650px;
    height: 100%;
    overflow: hidden;
  }
  
  .main {
    width: 96%;
    min-height: 600px;
    height: 100%;
    border: 1px solid #8492a6;
    border-top: 2px solid #58b7ff;
    border-radius: 3px;
    box-shadow: 0px 5px 5px #d3dce6;
  }
  
  .nav {
    height: 0px;
    width: 100%;
    border-bottom: 1px solid #8492a6;
  }
  
  .cont {
    width: 100%;
    min-height: 600px;
    height: 100%;
  }
  
  .cont>ul>li {
    display: flex;
    border-bottom: 1px solid #8492a6;
    height: 45px;
    line-height: 45px;
    text-align: center;
    overflow: hidden;
  }
  
  .cont>ul>li>div {
    width: 25%;
    border-left: 1px solid #8492a6;
    padding-left: 2px;
    padding-right: 2px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  
  .cont>ul>li>div:nth-child(1) {
    border-left: 0;
    width: 25%;
  }
  
  .cont>ul>li>div:nth-child(2) {
    width: 12%;
  }
  
  .cont>ul>li>div:nth-child(3) {
    width: 11%;
  }
  
  .cont>ul>li>div:nth-child(4) {
    width: 12%;
  }
  
  .cont>ul>li>div:nth-child(5) {
    width: 12%;
  }
  
  .cont>ul>li>div:nth-child(6) {
    width: 9%;
  }
  
  .cont>ul>li>div:nth-child(7) {
    width: 11%;
  }
  
  .cont>ul>li>div:nth-child(8) {
    width: 8%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
  }
  
  .cont>ul .dataLi>div:last-child small {
    cursor: pointer;
    width: 65px;
    height: 25px;
    line-height: 27px;
    background-color: #f07d8c;
    color: #fff;
    border-radius: 15px;
    font-size: 14px;
    border: 1px solid #fff;
  }
  
  .cont>ul .dataLi>div:last-child small:hover {
    border: 1px solid #f07d8c;
  }
  
  .cont>ul .dataLi:hover {
    background-color: #E5E9F2;
  }
  
  .cont>ul .firstLi {
    background-color: #e5e9f2;
  }
  
  .cont>ul .nullLi>div:nth-child(1) {
    width: 100%;
    text-align: center;
  }
  
  .cont>ul .dataLi>div:last-child small span {
    font-size: 12px;
    margin-right: 3px;
    color: #fff
  }
  
  .elbut {
    color: #666;
    margin: 0;
    padding: 0;
  }
  
  .cont>ul .lastLi {
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  
  .cont>ul .lastLi>div:nth-child(1) {
    border-left: 0;
    margin-top: 15px;
    height: 45px;
    z-index: 1;
    width: 80%;
  }
  
  .cont>ul .lastLi>div:nth-child(2) {
    border-left: 0;
    margin-top: 15px;
    height: 45px;
    width: 10%;
  }
</style>